Param(
    [int]$Port = 8019
)

$prefix = "http://localhost:$Port/"
$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add($prefix)
$listener.Start()
Write-Output "Preview URL: $prefix"

while ($true) {
    $ctx = $listener.GetContext()
    $html = "<html><head><meta charset='utf-8'/><title>Preview</title></head><body>Preview server running. Open WeChat DevTools to view UI changes.</body></html>"
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($html)
    $ctx.Response.ContentType = 'text/html'
    $ctx.Response.OutputStream.Write($bytes,0,$bytes.Length)
    $ctx.Response.Close()
}